package org.ainlolcat.ainscience.statistics.handlers;

import org.ainlolcat.ainscience.UI.renderer.LineDataRenderer;
import org.ainlolcat.ainscience.UI.renderer.LineRendererSource;
import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.statistics.StatisticHandler;
import org.ainlolcat.ainscience.statistics.StatisticStorage;
import org.ainlolcat.ainscience.utils.ArraysUtil;
import org.ainlolcat.ainscience.utils.XMLUtils;
import org.jdom.Element;

import javax.swing.*;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

/**
 * @author ainlolcat
 * Date: 11/5/11
 */
public class NormalIntegralStatisticHandler implements StatisticHandler , LineRendererSource{
    Hashtable<String, double[][]> data = new Hashtable<String, double[][]>();
    int BASE_LINE_DEFAULT = 30;
    int AVERAGE_DOTS_DEFAULT = 0;

    public JPanel getRenderer() {
        return new LineDataRenderer(this);
    }

    public Hashtable<String,String> getStatistic(Entity entity, Integer columnID) {
        return getStatistic(entity, columnID, null);
    }

    public Hashtable<String, String> getStatistic(Entity entity, Integer columnID, Element params) {
        Hashtable<String,String> stats = new Hashtable<String, String>();
        int baseLineDots = XMLUtils.getSafeInteger4Attribute(params,"baseLineDots", BASE_LINE_DEFAULT );
        int avgIntegralDots = XMLUtils.getSafeInteger4Attribute(params,"avgIntegralDots", AVERAGE_DOTS_DEFAULT );
        Column cy = entity.getColumnByOrder(columnID);
        if (cy.isNumeric()){
            Column cx = entity.getXColumn(cy.getAxis());
            if (cx.isNumeric()){
                double[] x = cx.getDoubleRepresentation();
                double[] y = cy.getDoubleRepresentation();
                if (params == null || params.getChildren("range").isEmpty()) {
                    double start = x[0];
                    double end =  x[x.length-1];
                    String name = "Integral " + start + " " + end;
                    Double answer = integrateWholeRange(
                            x,
                            y,
                            baseLineDots,
                            avgIntegralDots,
                            name);
                    StatisticStorage.getInstance().registerHandler(entity,name,this);
                    stats.put(name, answer.toString());
                }
                else {
                    for (Element integral:(List<Element>)params.getChildren("range")){
                        //try retrieve settings from params
                        double start = XMLUtils.getSafeDouble4Attribute(integral, "start", x[0]);
                        double end = XMLUtils.getSafeDouble4Attribute(integral, "end", x[x.length-1]);
                        String name = XMLUtils.getSafeString4Attribute(integral, "name", "Integral " + start + " " + end);

                        Double answer = integrate(
                                x, y,
                                start, end,
                                baseLineDots,
                                avgIntegralDots,
                                name);
                        StatisticStorage.getInstance().registerHandler(entity,name,this);
                        stats.put(name, answer.toString());
                    }
                }
            }
        }
        return stats;
    }

    private double integrateWholeRange(double[] x, double[] y, int baseLineDots, int avgIntegralDots, String name){
        return integrate(x,y,x[0], x[x.length-1], baseLineDots, avgIntegralDots, name);
    }

    private double integrate(double[] x, double[] y, double start, double end,  int baseLineDots, int avgIntegralDots , String name){
        double[][] data = ArraysUtil.normIntegrateWithOutBaseLine(x, y, start, end, baseLineDots);

        this.data.put(name,data);
        int dataSetSize = data[0].length;

        if (avgIntegralDots > 0){
            if (avgIntegralDots > dataSetSize/3 ) {
                avgIntegralDots = dataSetSize/3;
            }
            double integral = 0;
            for (int i=0;i<avgIntegralDots;i++){
                integral += data[2][dataSetSize - 1 - i];
            }
            return integral/avgIntegralDots;
        } else {
            return data[2][dataSetSize-1];
        }
    }

    public Collection<String> getNames() {
        return data.keySet();
    }

    public double[][] getData(String name) {
        return data.get(name);
    }
}
